package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.activity.AdverActivity;
import com.fanhao.dllo.soundfm.activity.PlayActivity;
import com.fanhao.dllo.soundfm.adapter.RecomViewPageAdapter;
import com.fanhao.dllo.soundfm.entity.PlayActivityCommentsResult;
import com.fanhao.dllo.soundfm.entity.RecommendResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/12.
 * 轮播Adapter返回的Fragment
 */
public class RecommentViewPagerFragment extends BaseFragment {
    private NetworkImageView netIvRecViewPager;
    private ImageLoader imageLoader;
    private static List<RecommendResult.FocusImagesEntity.ListEntity> lists;

    public static RecommentViewPagerFragment getInstance(String imgUrl,
                                                         int position, List<RecommendResult.FocusImagesEntity.ListEntity> list) {
        RecommentViewPagerFragment recommentViewPagerFragment =
                new RecommentViewPagerFragment();
        lists = list;
        // 参数传递
        Bundle bundle = new Bundle();
        bundle.putString("imgUrl", imgUrl);
        bundle.putInt("position", position);
        recommentViewPagerFragment.setArguments(bundle);
        return recommentViewPagerFragment;
    }


    @Override
    protected int getLayoutView() {
        return R.layout.viewpager_recommentfragment;
    }

    @Override
    protected void initView() {
        netIvRecViewPager = findView(R.id.netIv_viewPagaerRecomment);
    }

    @Override
    protected void initData() {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            String url = bundle.getString("imgUrl");
            netIvRecViewPager.setImageUrl(url, imageLoader);
            final int pos = bundle.getInt("position");
            netIvRecViewPager.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 如果Type == 4,跳到广告页面
                    if (lists.get(pos).getType() == 4) {
                        Intent intent = new Intent();
                        intent.setClass(getActivity(), AdverActivity.class);
                        intent.putExtra("url", lists.get(pos).getUrl());
                        intent.putExtra("shortTitle", lists.get(pos).getShortTitle());
                        getActivity().startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.move_in_from_bottom, R.anim.no_move);
                    }
                    // 如果Type == 3,跳到playActivity界面
                    if (lists.get(pos).getType() == 3) {
                        Intent intent = new Intent();
                        intent.setClass(getActivity(), PlayActivity.class);
                        Bundle bundle = new Bundle();
                        intent.putExtra("trackId", lists.get(pos).getTrackId());
                        bundle.putBoolean("tag",true);
                        intent.putExtras(bundle);
                        getActivity().startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.move_in_from_bottom, R.anim.no_move);
                    }
                    // 如果Type == 2,跳到playListFragment界面
                    if (lists.get(pos).getType() == 2) {
                        goToNextPlayList(new PlayListFragment(lists.get(pos).getUid(),0));
                    }

                    // 如果Type == 9, subType == 1,跳到InforSubTypeOneFragment界面
                    if (lists.get(pos).getType() == 9 && lists.get(pos).getSubType() == 1) {
                        goToNextPlayList(new InforSubTypeOneFragment(lists.get(pos).getSpecialId()));
                    }
                    // 如果Type == 9, subType == 2,跳到InforSubTypeTwoFragment界面
                    if (lists.get(pos).getType() == 9 && lists.get(pos).getSubType() == 2) {
                        goToNextPlayList(new InforSubTypeTwoFragment(lists.get(pos).getSpecialId()));
                    }
                }
            });

        }
    }
}
